package ua.com.jpy.services.customer;

import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.PasswordEncoder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import ua.com.jpy.entity.IEntity;
import ua.com.jpy.entity.customer.Customer;
import ua.com.jpy.entity.customer.dao.ICustomerDao;
import ua.com.jpy.services.basic.AJpyBasicService;

/**
 * @author LSD25
 *
 */
@Service
public class CustomerService extends AJpyBasicService implements ICustomerService {
	
	private static final Log log = LogFactory.getLog(CustomerService.class);
	
	@Autowired
	private ICustomerDao customerDao;
	
	@Autowired
	private PasswordEncoder passwordEncoder;

	@Override
	public IEntity getOrCreate(IEntity entity, ObjectId id) {
		if(entity != null && id == null) {
			Customer customer = (Customer) entity;
			String password = customer.getPassword();
			customer.setPassword(passwordEncoder.encodePassword(password, null));
			entity = customer;
		}
		return customerDao.getOrCreate(entity, id);
	}
	
	@Override
	public boolean delete(IEntity entityFoDelete, ObjectId id) {
		return customerDao.delete(entityFoDelete, id);
	}
	
	@Override
	public boolean update(IEntity entityForUpdate, ObjectId id) {
		return customerDao.update(entityForUpdate, id);
	}

	@Override
	public Customer find(String login) {
		return customerDao.find(login);
	}
	
	@Override
	public Customer getCurrentCustomer() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication != null) {
			Object principal = authentication.getPrincipal();
			if (principal instanceof UserDetails) {
				Customer customer = null;
				try {
					User user = (User) principal;
					customer = find(user.getUsername());
					log.info("Customer found in cookie!!!");
				} catch(Exception exc) {
					log.error("Cannot cast customer:", exc);
				}
				if(customer == null) {
					log.error("Customer is null");
				}
				return customer;
			}
		}
		log.info("Customer can not found in cookie!!!");
		return null;
	}

	@Override
	public Collection<? extends GrantedAuthority> getCustomerRoles() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		Collection<? extends GrantedAuthority> roles = authentication.getAuthorities();
		return roles;
	}

	@Override
	public String getStringCustomerRoles() {
		String customerRoles = "";
		Collection<? extends GrantedAuthority> roles = getCustomerRoles();
		int i = 0;
		if(roles != null) {
			for(GrantedAuthority role : roles) {
				if(i != roles.size() - 1) {
					customerRoles += role.getAuthority() + " ";
				} else {
					customerRoles += role.getAuthority();
				}
			}
		}
		return customerRoles;
	}

}
